using System;
using System.Collections.Generic;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Reflection;

namespace System.ComponentModel.Composition.Hosting
{
	public static class MefExtensions
	{
		private static readonly MethodInfo GetExportedMethod = typeof (CompositionContainer)
			.GetMethods(BindingFlags.Instance |BindingFlags.Public)
			.First(m => m.Name == "GetExportedValue" && !m.GetParameters().Any());

		public static object GetExportedValue(this CompositionContainer container, Type type)
		{
			return GetExportedMethod.MakeGenericMethod(type).Invoke(container, null);
		}

		private static readonly MethodInfo GetExportedValuesMethod = typeof(CompositionContainer)
			.GetMethods(BindingFlags.Instance | BindingFlags.Public)
			.First(m => m.Name == "GetExportedValues" && !m.GetParameters().Any());

		public static IEnumerable<object> GetExportedValues(this CompositionContainer container, Type type)
		{
			return (IEnumerable<object>) GetExportedValuesMethod.MakeGenericMethod(type).Invoke(container, null);
		}
	}
}